package com.ftg.shopping.person.controller;


import com.ftg.shopping.config.LayuiPage;
import com.ftg.shopping.person.entity.sp_personinfo;
import com.ftg.shopping.person.service.personService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@Api(description="人员维护")
@RequestMapping("/api/person")
public class personController {

    @Autowired
    personService personService;

//    @ApiOperation("展示所有的人员信息")
//    @GetMapping("/show")
//    public List<sp_personinfo> showall(){
//        return personService.showAll();
//    }

//    @ApiOperation("按主键模糊查询人员信息")
//    @GetMapping("/findone/{personid}")
//    public sp_personinfo findone(@ApiParam("主键") @PathVariable int personid){
//        return personService.findOne(personid);
//    }

    @ApiOperation("按主键修改人员信息")
    @PostMapping("/update")
    public int updateperson(sp_personinfo sp){
        return personService.updatePerson(sp);
    }

    @ApiOperation("添加人员信息")
    @PostMapping("/insert")
    public Map<String,Object> insertperson(@RequestBody Part upload, sp_personinfo sp) throws IOException {

        //图片上传功能实现！为了文件可以常期存储，不便于存储到tomcat

        //添加数据到数据库（包含图片路径）
        //获得tomcat所在的路径 到 tomcat
        String path="C:\\Users\\Administrator\\Desktop\\nginx-1.19.4\\secound\\shangcheng\\res\\static\\img";

        String type =  upload.getContentType();

        String subix = type.substring(type.lastIndexOf("/")+1,type.length());

        String fileName =path+"/"+ UUID.randomUUID().toString()+"."+subix;

        upload.write(fileName);

        String dbFileName = UUID.randomUUID().toString()+"."+subix;
        sp.photourl=dbFileName;
        personService.insertPerson(sp);
        Map<String,Object> map=new HashMap<>();
        map.put("msg","success");
        map.put("code",0);
        map.put("src","");
        return map;
    }

    @ApiOperation("按主键删除人员信息")
    @DeleteMapping("/delete/{personid}")
    public int deleteperson(@ApiParam("主键") @PathVariable int personid){
        return personService.deletePerson(personid);
    }

//    @ApiOperation("按主键删除多个人员信息")
//    @DeleteMapping("deletes/{ids}")
//    public int deletepersons(@PathVariable List<Integer> ids){
//        return personService.deletePersons(ids);
//    }

//    @ApiOperation("layui分页查询带模糊")
//    @GetMapping("layuipage")
//    public LayuiPage<sp_personinfo> layuiPage(@RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String personname){
//        return personService.layuiPage(page,limit,personname);
//    }

    @ApiOperation("展示所有信息带模糊")
    @GetMapping("/showperson")
        public LayuiPage showperson(int page,int limit,String personname){
        return personService.showperson(page,limit,personname);
        }


}
